package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class EditAccountServlet extends HttpServlet {

    private static final String EditAccountForm = "/WEB-INF/jsp/account/EditAccountForm.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService accountService = new AccountService();
        HttpSession session = req.getSession();

        Account account = (Account)session.getAttribute("account");

        if (passwordsEqual(req, resp)){


            account.setPassword(req.getParameter("password"));
            account.setLastName(req.getParameter("account.lastName"));
            account.setFirstName(req.getParameter("account.firstName"));
            account.setEmail(req.getParameter("account.email"));
            account.setPhone(req.getParameter("account.phone"));
            account.setAddress1(req.getParameter("account.address1"));
            account.setAddress2(req.getParameter("account.address2"));
            account.setCity(req.getParameter("account.city"));
            account.setState(req.getParameter("account.state"));
            account.setZip(req.getParameter("account.zip"));
            account.setCountry(req.getParameter("account.country"));

            account.setLanguagePreference(req.getParameter("account.languagePreference"));
            account.setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
            account.setListOption(Boolean.parseBoolean(req.getParameter("account.listOption")));
            account.setBannerOption(Boolean.parseBoolean(req.getParameter("account.bannerOption")));

            accountService.updateAccount(account);
            session.setAttribute("message","修改信息成功");

            if (session.getAttribute("passwordNotEqualMSG")!=null){
                session.setAttribute("passwordNotEqualMSG",null);
            }


        }

        else {
            String passwordNotEqualMSG = "两次密码不一样！！！！";

            session.setAttribute("passwordNotEqualMSG",passwordNotEqualMSG);

        }



        if(account != null){
            HttpServletRequest httpRequest= req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();

            String logInfo = logService.logInfo(" ") + strBackUrl + account.getUsername()+" 更新了自己的信息";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        req.getRequestDispatcher(EditAccountForm).forward(req,resp);
    }

    boolean passwordsEqual(HttpServletRequest req, HttpServletResponse resp){
        return req.getParameter("password").equals(req.getParameter("repeatedPassword"));
    }
}
